<script setup lang="ts">
import OpeningRemarks from '@/views/agent/chat/components/OpeningRemarks.vue'
import UploadFile from '@/views/agent/chat/components/UploadFile.vue'
import CollectForm from '@/views/agent/chat/components/CollectForm.vue'
import CheckBox from '@/views/agent/chat/components/CheckBox.vue'
import StepLoading from '@/views/agent/chat/components/StepLoading.vue'
import { computed, ref } from 'vue'
import {
  ContractReviewProcessLoading,
  contractTypes,
  reviewFocus,
  contractAgentConfig,
  JDAgent
} from '@/views/agent/chat/components/data'
import applicationApi from '@/api/application'

const tip = 'AI 基于专业律师的思维方式逐步进行推理审查，适合不特定类型的审查任务'

const formConfig = [
  {
    title: '输入您的立场',
    description: '你所代表的主体名称，与合同内保持一致',
    slotName: 'one'
  },
  {
    title: '选择合同类型',
    description: '选定不同合同类型，会对应不同审查重点',
    slotName: 'two'
  },
  {
    title: '设置审查重点',
    description: '编辑设置合同重点，影响代表方的审查倾向',
    slotName: 'three'
  },
]

const props = withDefaults(
  defineProps<{
    appId?: any
    chatId?: string
    applicationDetails: any
  }>(),
  {
  }
)

const step = ref(0)

const topTips = ref(tip)

const file = ref()
const formData: any = ref({
  position: '', // 立场
  supplement: '', // 补充说明
})
const setDocumentList = (val: any) => {
  file.value = val
  topTips.value = val[0].name
  step.value = 1;
}

function onBack () {
  step.value = 0;
}

const resultContent = ref('--')
const loading = ref(false)
async function onSubmit () {
  try {
    step.value = 2;
    loading.value = true
    const chatData = await applicationApi.getChatOtherOpen(contractAgent.value.appId, contractAgent.value.apiKey)
    const fm = new FormData()
    for (const f of file.value) {
      fm.append('file', f.raw, f.name)
    }
    const fileData = await applicationApi.uploadFile(contractAgent.value.appId, chatData.data, fm);
    const message = `立场名称:${formData.value.position};补充审查重点:${formData.value.supplement};`
    const data = await applicationApi.postChatMessageNew(chatData.data, { document_list: fileData.data, stream: false, message: message, apiKey: JDAgent.apiKey })
    resultContent.value = data.data?.content || '--'
  } catch (e) {
    console.log(e)
  } finally {
    loading.value = false;
  }
}

const hasSubmit = computed(() => {
  return step.value === 1 && formData.value.position && formData.value.type && file.value.length > 0 && !loading.value
})
const reviewClassData = ref<any>([])

const contractAgent = ref({
  appId: '',
  apiKey: '',
})
function reviewClass(val: number) {
  loading.value = true
  reviewClassData.value = []
  setTimeout(() => {
    reviewClassData.value = reviewFocus[val]
    contractAgent.value = contractAgentConfig[val]
    loading.value = false
  }, 1000)
}

const active = computed(() => {
  if (formData.value.position && !formData.value.type && !formData.value.supplement) {
    return 1
  }
  if (formData.value.position && formData.value.type && !formData.value.supplement) {
    return 2
  }
  if (formData.value.position && formData.value.type && formData.value.supplement) {
    return 3
  }
  return 0
})

</script>

<template>
  <OpeningRemarks class="mb-12"  :title="props.applicationDetails.name" :description="props.applicationDetails.desc" :avatar="props.applicationDetails.avatar"/>
  <el-card class="mt-12 mb-12" shadow="never" v-if="step !== 2">
    <div class="ai-start">
      <div class="ai-start-tips">
        <el-icon @click="onBack" v-if="step === 1"><ArrowLeftBold /></el-icon>
        <div class="ai-start-tips-ellipsis">
          {{ topTips }}
        </div>
      </div>
      <el-button type="primary" size="small" @click="onSubmit" :disabled="!hasSubmit">开始审查</el-button>
    </div>
  </el-card>
  <UploadFile v-if="step === 0" :app-id="props.appId" :showFileList="false" @update="setDocumentList" :max-files="1" name="合同" tip="支持格式：doc、docx格式文件" accept=".docx,.doc" />
  <CollectForm v-if="step === 1" :data="formConfig" :active="active">
    <template #one>
      <el-input v-model="formData.position" placeholder="请输入" />
    </template>
    <template #two>
      <el-select v-model="formData.type" placeholder="请选择" @change="reviewClass">
        <el-option v-for="(item, index) in contractTypes" :key="index" :label="item.label" :value="item.value" />
      </el-select>
    </template>
    <template #three>
      <div v-loading="loading" element-loading-text="加载中...">
        <CheckBox :data="reviewClassData"/>
        <el-input v-model="formData.supplement" type="textarea" :rows="4" placeholder="如需补充审查重点，请填写" />
      </div>
    </template>
  </CollectForm>
  <StepLoading v-if="step === 2" :data="ContractReviewProcessLoading" :loading="loading" :resultContent="resultContent" :applicationDetails="props.applicationDetails" />
</template>

<style scoped lang="scss">

</style>
